﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Core.Base;
using System.ComponentModel;
using Base;
using System.Drawing;
using Core.Component;
using Core;

namespace Components
{
    //public class SGVTextBox: MaskedTextBox, IBindingControl, ISGVControl
    public class SGVMaskedTextBox : MaskedTextBox, IBindingControl, ISGVControl
    {
        public SGVMaskedTextBox()
            :base()
        {
            this.CausesValidation = false;
            this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.ShortcutsEnabled = true;
            this.GotFocus += new EventHandler(SGVComboBox_GotFocus);
            this.LostFocus += new EventHandler(SGVComboBox_LostFocus);
            this.TextChanged += new EventHandler(SGVTextBox_TextChanged);
            this.KeyUp += SGVTextBox_KeyUp;
            

        }

        public Core.Component.UIComponent component
        {
            set;
            get;
        }

        void SGVTextBox_TextChanged(object sender, EventArgs e)
        {
            if (this.Focused == false)
            {
            }
        }

        void SGVTextBox_KeyUp(object sender, KeyEventArgs e)
        {
            //if (!string.IsNullOrEmpty(this.Text))
            //{
            //    try
            //    {
            //        System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("vi-VN");
            //        float valueBefore = float.Parse(this.Text, System.Globalization.NumberStyles.AllowThousands);
            //        this.Text = String.Format(culture, "{0:0,0.00}", valueBefore);
            //        this.Select(this.Text.Length, 0);
            //    }
            //    catch (Exception ex)
            //    {

            //    }

            //}
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys.Control | Keys.A))
            {
                this.SelectAll();
            }
            else if (keyData == (Keys.Control | Keys.C))
            {
                this.Copy();
            }
            else if (keyData == (Keys.Control | Keys.V))
            {
                this.Paste();
            }
            else if (keyData == (Keys.Control | Keys.X))
            {
                this.Cut();
            }
            else if (keyData == (Keys.Control | Keys.Z))
            {
                this.Undo();
            }
            else if (keyData == Keys.Enter)
            {
                SendKeys.Send("{TAB}");
            }

            return base.ProcessCmdKey(ref msg, keyData);
        }

        void SGVComboBox_LostFocus(object sender, EventArgs e)
        {
            SGVControlHelper.ControlLostFocus(this);
        }

        void SGVComboBox_GotFocus(object sender, EventArgs e)
        {
            SGVControlHelper.ControlFocus(this);
            this.SelectAll();
        }


        #region IBindingControl
        public BaseModule module;
        public BaseEntity entity;
        public String _bindingPropertyName = "Text";

        String _bindingTable;
        String _bindingColumn;
        String _dataSourceTable;
        String _dataSourceDisplayColumn;
        String _dataSourceKeyColumn;
        String _dataSourceColumns;
        String _sGVShortcutString;
        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public String SGVShortcutString
        {
            get { return _sGVShortcutString; }
            set { _sGVShortcutString = value; }
        }
        String _sGVTag;
        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string SGVTag
        {
            get
            {
                return _sGVTag;
            }
            set
            {
                _sGVTag = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string BindingPropertyName
        {
            get
            {
                return _bindingPropertyName;
            }
            set
            {
                _bindingPropertyName = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string BindingTable
        {
            get
            {
                return _bindingTable;
            }
            set
            {
                _bindingTable = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string BindingColumn
        {
            get
            {
                return _bindingColumn;
            }
            set
            {
                _bindingColumn = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string DataSourceTable
        {
            get
            {
                return _dataSourceTable;
            }
            set
            {
                _dataSourceTable = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string DataSourceDisplayColumn
        {
            get
            {
                return _dataSourceDisplayColumn;
            }
            set
            {
                _dataSourceDisplayColumn = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string DataSourceKeyColumn
        {
            get
            {
                return _dataSourceKeyColumn;
            }
            set
            {
                _dataSourceKeyColumn = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public String DataSourceColumns
        {
            get
            {
                return _dataSourceColumns;
            }
            set
            {
                _dataSourceColumns = value;
            }
        }

        public void RegisterModule(Core.Base.BaseModule _module)
        {
            if (_module != null)
            {
                this.module = _module;
                this.entity = _module.entity;
            }
        }

        #endregion

        public virtual void Init()
        {
            this.Font = new Font(AppContext.FontString, this.Font.Size + 0.3f, this.Font.Style);
        }


        public void UpdateFormat()
        {
            
        }

        public UIComponent Serialize()
        {
            UIComponent component = new UIComponent();
            SGVControlHelper.CreateUIComponentFromControl(component, this);

            if (component != null)
            {
                component.Value = this.Text;
            }
            return component;
        }

        public void Deserialize(UIComponent component)
        {
            throw new NotImplementedException();
        }
    }
}
